<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : pageType === 'detail' ?'修改': '详情'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="用户名" prop="username">
        <el-input disabled="pageType === 'detail'" v-model="dataForm.username" placeholder="登录帐号"></el-input>
      </el-form-item>
      <el-form-item v-if="pageType !== 'detail'" label="密码" prop="password" :class="{ 'is-required': !dataForm.id }">
        <el-input disabled="pageType === 'detail'" v-model="dataForm.password" type="password" placeholder="密码"></el-input>
      </el-form-item>
      <el-form-item v-if="pageType !== 'detail'" label="确认密码" prop="comfirmPassword" :class="{ 'is-required': !dataForm.id }">
        <el-input disabled="pageType === 'detail'" v-model="dataForm.comfirmPassword" type="password" placeholder="确认密码"></el-input>
      </el-form-item>
      <el-form-item label="年龄" prop="age">
        <el-input disabled="pageType === 'detail'" v-model="dataForm.age" placeholder="年龄"></el-input>
      </el-form-item>
      <el-form-item label="账户余额" prop="balance">
        <el-input disabled="pageType === 'detail'" v-model="dataForm.balance" placeholder="账户余额"></el-input>
      </el-form-item>
      <el-form-item label="身份证号" prop="idCard">
        <el-input disabled="pageType === 'detail'" v-model="dataForm.idCard" placeholder="身份证号"></el-input>
      </el-form-item>
      <el-form-item label="真实姓名" prop="realName">
        <el-input disabled="pageType === 'detail'" v-model="dataForm.realName" placeholder="真实姓名"></el-input>
      </el-form-item>
      <el-form-item label="手机号码" prop="telephoneNum">
        <el-input disabled="pageType === 'detail'" v-model="dataForm.telephoneNum" placeholder="手机号码"></el-input>
      </el-form-item>
      <el-form-item label="是否逾期" size="mini" prop="isOverdue">
        <el-radio-group disabled="pageType === 'detail'"  v-model="dataForm.isOverdue">
          <el-radio :label="true">是</el-radio>
          <el-radio :label="false">否</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="是否工作" size="mini" prop="notHasJob">
        <el-radio-group disabled="pageType === 'detail'"  v-model="dataForm.notHasJob">
          <el-radio :label="true">是</el-radio>
          <el-radio :label="false">否</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="是否失信" size="mini" prop="isDishonest">
        <el-radio-group disabled="pageType === 'detail'"  v-model="dataForm.isDishonest">
          <el-radio :label="true">是</el-radio>
          <el-radio :label="false">否</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <span v-if="pageType !== 'detail'" slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import { isEmail, isMobile } from '@/utils/validate'
  export default {
    data () {
      var validatePassword = (rule, value, callback) => {
        if (!this.dataForm.id && !/\S/.test(value)) {
          callback(new Error('密码不能为空'))
        } else {
          callback()
        }
      }
      var validateComfirmPassword = (rule, value, callback) => {
        if (!this.dataForm.id && !/\S/.test(value)) {
          callback(new Error('确认密码不能为空'))
        } else if (this.dataForm.password !== value) {
          callback(new Error('确认密码与密码输入不一致'))
        } else {
          callback()
        }
      }
      var validateEmail = (rule, value, callback) => {
        if (!isEmail(value)) {
          callback(new Error('邮箱格式错误'))
        } else {
          callback()
        }
      }
      var validateMobile = (rule, value, callback) => {
        if (!isMobile(value)) {
          callback(new Error('手机号格式错误'))
        } else {
          callback()
        }
      }
      return {
        visible: false,
        roleList: [],
        pageType: '',
        dataForm: {
          age: 28,
          balance: 1000000,
          gmtCreate: '',
          gmtModified: '',
          idCard: '',
          isDeleted: false,
          isDishonest: false,
          isOverdue: false,
          notHasJob: false,
          password: '',
          realName: '',
          telephoneNum: '',
          userId: '',
          username: ''
        },
        dataRule: {
          userName: [
            { required: true, message: '用户名不能为空', trigger: 'blur' }
          ],
          password: [
            { validator: validatePassword, trigger: 'blur' }
          ],
          comfirmPassword: [
            { validator: validateComfirmPassword, trigger: 'blur' }
          ],
          email: [
            { required: true, message: '邮箱不能为空', trigger: 'blur' },
            { validator: validateEmail, trigger: 'blur' }
          ],
          mobile: [
            { required: true, message: '手机号不能为空', trigger: 'blur' },
            { validator: validateMobile, trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id, type) {
        this.dataForm.id = id || 0
        this.pageType = type || ''
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
        })
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/sys/app/user/info/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.age = data.user.age
              this.dataForm.username = data.user.username
              this.dataForm.balance = data.user.balance
              this.dataForm.gmtCreate = data.user.gmtCreate
              this.dataForm.gmtModified = data.user.gmtModified
              this.dataForm.idCard = data.user.idCard
              this.dataForm.isDeleted = data.user.isDeleted
              this.dataForm.isDishonest = data.user.isDishonest
              this.dataForm.isOverdue = data.user.isOverdue
              this.dataForm.notHasJob = data.user.notHasJob
              this.dataForm.realName = data.user.realName
              this.dataForm.telephoneNum = data.user.telephoneNum
              this.dataForm.userId = data.user.userId
            }
          })
        }
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sys/user/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'userId': this.dataForm.id || undefined,
                'username': this.dataForm.userName,
                'password': this.dataForm.password,
                'salt': this.dataForm.salt,
                'email': this.dataForm.email,
                'mobile': this.dataForm.mobile,
                'status': this.dataForm.status,
                'roleIdList': this.dataForm.roleIdList
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
